<template>
	<view class="set-up">
		<view class="su-main">
			<view class="item item-b" v-for="(item, index) in setUpList" :key="index" @click="onSetUp(item, index)">
				<view class="left">{{ item.text }}</view>
				<view class="right">
					<text v-if="item.id == 2">{{ CACHE_SIZE }}M</text>
					<text v-else-if="item.id == 3">当前版本{{ VERSION }}</text>
					<!-- <text v-if="item.id == 2">{{ CACHE_SIZE }}M</text> -->
					<!-- <text v-else-if="item.id == 3">{{ VERSION_CODE }}</text> -->
					<image src="/static/xs_icon/right3.png" mode="widthFix"></image>
				</view>
			</view>
		</view>

		<view class="pd-buttom">
			<view class="pdb-con" @click="onReturn()">退出登录</view>
		</view>

		<u-modal @confirm="confirm" @cancel="cancel" cancel v-model="show" :content="content" :title="title"
			:show-title="false" cancel-text="取消" confirm-text="同意" :show-cancel-button="true" :mask-close-able="true">
		</u-modal>
		<u-modal @confirm="loginOut" @cancel="cancel" cancel v-model="login" content="确定要退出系统吗？" title="退出登录"
			:show-title="false" cancel-text="取消" confirm-text="确认" :show-cancel-button="true" :mask-close-able="true">
		</u-modal>

		<successPop :showPopup="showPopup" :showTitle="'清除成功'" @confirm="determine" @clearData="clearData"></successPop>
	</view>
</template>

<script>
	// import { mapState, mapMutations } from "vuex";
	// import { VERSION_CODE } from '@/common/config.js';
	import successPop from '@/components/xs-components/success-pop/success-pop.vue';
	export default {
		components: {
			successPop
		},
		data() {
			return {
				CACHE_SIZE: 0, // 缓存
				VERSION: '',
				VERSION_CODE: '1.0.2', // 版本号
				type: '', // type  0 缓存 1 版本号
				show: false, // 弹窗
				title: '清除缓存', // 弹窗标题
				content: '是否清除缓存?', // 弹窗内容
				login: false,
				setUpList: [{
						text: '账号安全',
						id: '0',
						arrow: false
					},
					{
						text: '常见问题与说明',
						id: '1',
						arrow: false
					},
					{
						text: '清除缓存',
						id: '2',
						arrow: true
					},
					{
						text: '版本更新',
						id: '3',
						arrow: true
					},
					{
						text: '关于我们',
						id: '4',
						arrow: false
					},
					{
						text: '用户协议',
						id: '5',
						arrow: false
					},
					{
						text: '隐私政策',
						id: '6',
						arrow: false
					}
				],
				showPopup: false, // 清除成功弹窗
				upload_apk: "",
				update_type: ""
			};
		},
		onLoad() {
			this.CACHE_SIZE = uni.getStorageSync('CACHE_SIZE') || 0;
			// #ifdef APP-PLUS
			// 获取版本号
			plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
				// console.log(JSON.stringify(wgtinfo));
				console.log(wgtinfo); //应用版本号
				this.VERSION = wgtinfo.version;
				this.VERSION_CODE = wgtinfo.versionCode;
			});
			// #endif

			this.into();
		},
		methods: {
			// ...mapMutations(['getAddress','auoutUS']),
			into() {
				// this.postAddressList();
				// this.aboutUS();
			},
			// 获取地址列表
			postAddressList() {
				this.$api
					.post(global.apiUrls.setUP.addressList)
					.then(res => {
						// console.log(res.data)
						if (res.data.code == 1) {
							this.getAddress(res.data.data);
						}
					})
					.catch(err => {
						// this.$popup.showToast('保存失败')
					});
			},
			// 列表点击事件
			onSetUp(item, index) {
				// console.log(item,index)
				// 0 账号安全 1 常见问题与说明 2 清除缓存 3 版本更新 4 关于我们
				switch (item.id) {
					case '0':
						uni.navigateTo({
							url: '../security/security'
						});
						break;
					case '1':
						uni.navigateTo({
							url: '../manual/manual'
						});
						break;
					case '2':
						console.log('清除缓存');
						this.show = true;
						this.type = 0;
						this.content = '是否清除缓存？'; // 弹窗内容
						break;
					case '3':
						console.log('当前版本' + this.VERSION_CODE);
						this.versionUpdate();
						break;
					case '4':
						// console.log('关于我们');
						uni.navigateTo({
							url: '../about-us/about-us'
						});
						break;
					case '5':
						uni.navigateTo({
							url: '/pages/agreement'
						})
						break;
					case '6':
						uni.navigateTo({
							url: '/pages/agreement?type=' + 1
						})
						break;	
				}
			},
			// 版本更新
			versionUpdate() {
				this.$api
					.post(global.apiUrls.versionUpdate)
					.then(res => {
						console.log(res.data);
						if (res.data.code == 1) {
							console.log(this.VERSION_CODE * 1);
							console.log(res.data.data.version * 1);

							this.update_type = res.data.data.update_type;

							if (this.VERSION_CODE * 1 < res.data.data.version * 1) {
								this.show = true;
								this.type = 1;
								this.content = '系统当前有最新版本，确定更新吗？'; // 弹窗内容
								if (this.update_type == 'wgt') {
									this.upload_apk = res.data.data.is_update_apk;
								} else {
									this.upload_apk = res.data.data.upload_apk;
								}
							} else {
								this.$message.info('已经是最新版本');
							}
						}
					})
					.catch(err => {
						// this.$popup.showToast('保存失败')
					});
			},
			clearData() {
				// 获取缓存数据大小
				const res = uni.getStorageInfoSync();
				this.currentSize = res.currentSize
				console.log(this.currentSize)
			},
			// 同意
			confirm(e) {
				if (!e) {
					const res = uni.getStorageInfoSync();
					this.currentSize = res.currentSize
					this.CACHE_SIZE = 0
					// console.log(this.currentSize)
				} else {
					console.log('同意');
					plus.nativeUI.showWaiting('安装更新文件...');
					// this.$popup.showLoading('正在更新请稍后...');
					this.toUpdate();
				}
				
				// setTimeout(() => {

				// }, 3000);
			},
			
			//  版本更新
			toUpdate() {
				console.log(this.upload_apk)
				console.log(this.update_type)
				if (this.update_type == 'apk') {
					plus.nativeUI.closeWaiting();
					return plus.runtime.openURL(this.upload_apk);
				}

				let _this = this;
				uni.downloadFile({
					url: this.upload_apk, //仅为示例，并非真实的资源
					success: res => {
						uni.hideLoading();
						console.log(res.tempFilePath);
						if (res.statusCode === 200) {
							plus.runtime.install(
								res.tempFilePath, {
									force: true
								},
								function() {
									plus.nativeUI.closeWaiting();
									// plus.nativeUI.alert('应用资源更新完成！', function() {});
									_this.$util.showModal({
										title: '提示',
										content: '应用资源更新完成！',
										confirmText: '重启',
										success: res => {
											if (res.confirm) {
												plus.runtime.restart();
											}
										},
									})
									// plus.runtime.restart();
								},
								function(e) {
									console.log(e)
									plus.nativeUI.closeWaiting();
									plus.nativeUI.alert('安装更新文件失败[' + e.code + ']：' + e.message);
									if (e.code == 10) {
										alert('请清除临时目录');
									}
								}
							);

							// plus.runtime.install(res.tempFilePath, {
							// 	force: true
							// });
							// plus.runtime.restart();

							this.$message.info('更新完毕');
						} else {
							this.$message.info('更新失败');
						}
					}
				});
			},
			// 取消
			cancel() {
				console.log('取消');
			},
			// 确定弹窗
			determine(e) {
				console.log(e)
				this.showPopup = e;
			},
			// 退出
			onReturn() {
				this.login = true;
			},
			loginOut() {
				uni.removeStorageSync('USER_INFO');
				uni.removeStorageSync('USER_TOKEN');
				uni.reLaunch({
					url: '/pages/login/login/index'
				});
			}
		}
	};
</script>

<style lang="scss">
	@import './index.scss';
</style>
